import { Injectable } from '@nestjs/common'
import { CreateWireDto } from './dto/create-wire.dto'
import { UpdateWireDto } from './dto/update-wire.dto'
import { PrismaService } from 'src/prisma/prisma.service'

@Injectable()
export class WireService {
  constructor(private prisma: PrismaService) {}
  create(createWireDto: CreateWireDto) {
    return this.prisma.wire.create({ data: createWireDto })
  }

  async findAll(args: Record<string, any>) {
    const { sectionId, search, searchStyle } = args

    if (!sectionId) return this.prisma.wire.findMany()

    return this.prisma.wire.findMany({
      where: {
        sectionId: +sectionId,
        wireName: { contains: search },
        wireStyle: { contains: searchStyle },
      },
    })
  }

  async findOne(id: number) {
    return await this.prisma.wire.findUnique({
      where: { id },
    })
  }

  update(id: number, updateWireDto: UpdateWireDto) {
    return `This action updates a #${id} wire`
  }

  remove(id: number) {
    return `This action removes a #${id} wire`
  }

  /**
   * 删除所有数据
   * @param id 区段的编号
   * @returns 删除的数据个数
   */
  async removeAll(id: number) {
    return await this.prisma.wire.deleteMany({
      where: { sectionId: id },
    })
  }
}
